using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace StupidSpaceThing
{
	internal class MultiTextGuide : Guide
	{		
		private float scale;
		private SpriteFont font;
		private List<List<TextBite>> lines = new List<List<TextBite>>();		

		public MultiTextGuide(SpriteFont font, float scale, params TextBite[] bites)
			: base(0, null) {
			this.scale = scale;
			this.font = font;
			int line = 0;
			lines.Add(new List<TextBite>());
			for( int i = 0; i < bites.Length; i++ ) {
				lines[line].Add(bites[i]);
				if( bites[i].Endline ) {
					line++;
					lines.Add(new List<TextBite>());
				}
			}
		}

		public class TextBite
		{
			public readonly string Text;
			public readonly Color Color;
			public readonly bool Endline;

			public TextBite(string text) : this(text, Color.White, false) {}
			public TextBite(string text, bool endline) : this(text, Color.White, endline) { }
			public TextBite(string text, Color color) : this(text, color, false) { }
			public TextBite(string text, Color color, bool endline) {
				this.Text = text;
				this.Color = color;
				this.Endline = endline;
			}
		}

		public override void Draw(SpriteBatch spriteBatch) {
			int line = 0;
			foreach( List<TextBite> bites in lines ) {
				if( bites.Count > 0 ) {
					float totalWidth = 0;
					foreach( TextBite bite in bites ) {
						totalWidth += font.MeasureString(bite.Text).X * scale;
					}
					int leftOffset = (int)(spriteBatch.GraphicsDevice.Viewport.Width / 2 - (totalWidth) / 2);
					int topOffset = (int)(spriteBatch.GraphicsDevice.Viewport.Height / 2 - (font.MeasureString(bites[0].Text).Y * scale * ((lines.Count / 2 - line) * 2)) / 2);

					float curOffset = 0;
					foreach( TextBite bite in bites ) {
						spriteBatch.DrawString(font, bite.Text, new Vector2(leftOffset + curOffset - 1, topOffset - 1), GetColor(Color.Black), 0, Vector2.Zero, scale, SpriteEffects.None, 0);
						spriteBatch.DrawString(font, bite.Text, new Vector2(leftOffset + curOffset, topOffset), GetColor(bite.Color), 0, Vector2.Zero, scale, SpriteEffects.None, 0);
						curOffset += font.MeasureString(bite.Text).X * scale;
					}
				}
				line++;
			}
			base.Draw(spriteBatch);
		}

	}
}
